// sxy bless pr
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a1[100005], a[100005], b[100005], q[100005];
int d[100005][32], f[100005][32], x[4];
bool pos[9];
void dfs(int now)
{
    if (now > (1 << k))
    {
        for (int i = 1; i <= n; i++)
            f[i][0] = i;
        for (int i = 1; i <= k; i++)
        {
            for (int j = 1; j <= (1 << (k - i)); j++)
            {
                if (d[i][j] == 0)
                {
                    if (b[f[(j - 1) * 2 + 1][i - 1]] >= i)
                        f[j][i] = f[(j - 1) * 2 + 1][i - 1];
                    else
                        f[j][i] = f[j * 2][i - 1];
                }
                else if (b[f[j * 2][i - 1]] >= i)
                    f[j][i] = f[j * 2][i - 1];
                else
                    f[j][i] = f[(j - 1) * 2 + 1][i - 1];
            }
        }
        pos[f[1][k]] = 1;
        return;
    }
    for (int i = 0; i <= k; i++)
    {
        b[now] = i;
        dfs(now + 1);
    }
}
int solve(int id)
{
    memset(pos, 0, sizeof(pos));
    for (k = 1; (1 << k) <= q[id]; k++)
        ;
    for (int i = 1; i <= q[id]; i++)
        b[i] = a[i];
    dfs(q[id] + 1);
    int ans = 0;
    for (int i = 1; i <= n; i++)
        if (pos[i])
            ans += i;
    return ans;
}
int main()
{
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a1[i];
    for (int i = 1; i <= m; i++)
        cin >> q[i];
    string s;
    for (k = 1; (1 << k) <= n; k++)
        ;
    cout << k << endl;
    for (int i = 1; i <= k; i++)
    {
        cin >> s;
        for (int j = 1; j <= 1 << (k - i); j++)
            d[j][i] = s[j - 1] - '0';
    }
    int t, ans;
    cin >> t;
    for (int i = 1; i <= t; i++)
    {
        for (int j = 0; j <= 3; j++)
            cin >> x[j];
        for (int j = 1; j <= n; j++)
            a[j] = (a1[j] ^ x[j % 4]);
        ans = 0;
        for (int j = 1; j <= m; j++)
            ans = (ans ^ (solve(j) * j));
        cout << ans << endl;
    }
    return 0;
}
